import { Component, OnInit } from '@angular/core';
import { ToastService, UploadConfig, Item, ConfirmService } from 'ng-tui';
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { GlobalService } from '../../../../cores/services';
import { GoodsSpecification, Warehous } from '../../interfaces/warehous.interface';
import { ApiData } from '../../../../cores/classes';
import { skipWhile, switchMap } from 'rxjs/operators';
import { WarehousService } from '../../services/warehous.service';

@Component({
    selector: 'app-user-detail',
    templateUrl: './user-detail.component.html',
    styles: [
        `textarea{min-height:5rem;}`
    ]
})
export class UserDetailComponent implements OnInit {

    defaultTh = ['单价', '库存', '是否使用'];

    parentTypes = new Array<Item>();

    childTypes = new Array<any>();

    warehous: Warehous = { id: 0, };
    warehouse = new Array<any>();
    position = new Array<any>();
    Channe = new Array<any>();
    options: UploadConfig;

    private stockID: number;
    constructor(
        private router: Router,
        public global: GlobalService,
        private active: ActivatedRoute,
        private toast: ToastService,
        private goodsService: WarehousService,
        private confirm: ConfirmService,
    ) {
        this.options = {
            queryString: '?x-oss-process=image/resize,h_130,w_130',
            uploader: (file: File) => this.goodsService.uploadGoodsImage(file)
        };
        this.active.paramMap.
            pipe(skipWhile(params => !params.has('id')), switchMap<ParamMap, ApiData>(params => {
                this.warehous.id = parseInt(params.get('id'), 10);
                return this.goodsService.deldetlist(this.warehous.id);
            }))
            .subscribe(res => {
                this.warehous = res.datas;

            });
    }

    ngOnInit() {
        this.departmentLista();
    }


    /**
     * 确认添加
     */
    confirmInsert(btn: any) {
        this.goodsService.insertLocation(this.warehous).subscribe({
            next: res => {
                this.toast.success('添加成功', `成功添加库位${this.warehous.warehouseLocationName}`);
                this.warehous.id = res.datas.id;
            },
            complete: () => {
                btn.dismiss();
            }
        });
    }
    confirmUpdate(btn: any) {
        this.goodsService.updateLocation(this.warehous).subscribe({
            next: res => {
                this.toast.success('修改成功', `成功修改库位${this.warehous.warehouseLocationName}的信息`);
            },
            complete: () => {
                btn.dismiss();
            }
        });
    }

    /**
       * 仓库下拉列表
       */
    departmentLista() {
        this.goodsService.addhouse()
            .subscribe({
                next: res => {
                    this.warehouse = res.datas;
                    this.warehouse.forEach(element => {
                        if (element.id === this.warehous.warehouseId) {
                            this.getPosition(element.id);
                        }
                    });
                },
            });
    }
    /**
     * 通过仓库获库区
     * @param id
     */
    getPosition(id) {
        this.goodsService.getPerson(id)
            .subscribe({
                next: res => {
                    this.position = res.datas;
                    this.position.forEach(element => {
                        if (element.id === this.warehous.warehouseRegionId) {
                            this.getwarehouseChanne(element.id);
                        }
                    });
                },
            });
    }

    /**
        * 通过仓库获库区
        * @param id
        */
    getwarehouseChanne(id) {
        this.goodsService.warehouseChanne(id)
            .subscribe({
                next: res => {
                    this.Channe = res.datas;
                },
            });
    }

}
